<?php
/**
 * HTML page for saving and editing blogposts 
 *
 * @version 0.1 
 * @license MIT License http://www.opensource.org/licenses/mit-license.php
 * @copyright (c) 2007 Geir Arne Brevik
 * @author Geir Arne Brevik
 */
include_once('lib.php'); // include bootstrapping and helper functions

// set the edit flag
$edit = (isset($_GET['s']) and slugExists($_GET['s'], 'posts')) ? true : false;
if ($edit){
	$blogpost = new MiniBlog(getIdFromField('posts', 'slug', $_GET['s']));
	// get saved data before editing
	if (!$_POST) { $form_values = $blogpost->getData(); }
}

$doc_title = $edit ? sprintf('Editing &laquo;%s&raquo;', $blogpost->title) : 'Create new blogpost';

include('meta.php');
include('header.php');

if ($edit){
	?><p><a href="<?php echo($blogpost->permalink)?>"><?php printf('See &laquo;%s&raquo; published', $blogpost->title); ?></a></p><?php
}

// display error message
if ($form_errors){
	?><p>Could not process. <?php echo($form_errors); ?></p><?php
}
?>
	<form action="<?php echo(isset($_GET['s']) ? '?s='.$blogpost->slug:'?'); ?>" method="POST">
		<input type="hidden" name="where" value="miniblog">
		<input type="hidden" name="action" value="save">
		<?php if($edit){ ?><input type="hidden" name="miniblog[id]" value="<?php echo($blogpost->id); ?>"><?php } ?>
		<label for="title">Title</label>
		<input type="text" name="miniblog[title]" id="title" value="<?php 
			echo((isset($form_values['title']) ? $form_values['title'] : false)); ?>">
		<label for="body">Body</label>
		<textarea name="miniblog[body]" id="body" rows="8" cols="40"><?php 
			echo((isset($form_values['body']) ? $form_values['body'] : false)); ?></textarea>
		<input type="checkbox" id="frontpage_allowed" name="miniblog[frontpage_allowed]" value="<?php echo(($form_values['frontpage_allowed'] ? $form_values['frontpage_allowed'] : 1)); ?>"<?php echo((!$edit or $blogpost->frontpage_allowed == 1) ? ' checked="checked"' : ''); ?>> <label for="frontpage_allowed">Allow to be shown on frontpage</label>
		<input type="submit" value="Save">
	</form>
<?php
include('footer.php');
?>